﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace ReplaceStringTools
{
    public partial class frmMain : Form
    {
        private static IDictionary<string, Func<string, string>> funcDicList = new Dictionary<string, Func<string, string>>
        {
            { ".get__Default",Replaceget__Default},
            { ".set__Default",Replaceset__Default},
            { ".set_Value2",Replaceset__Value2},
            { ".set_Name",Replaceset_Name},
        };



        public frmMain()
        {
            InitializeComponent();
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog sfd = new OpenFileDialog())
            {
                sfd.Filter = "CS文件(*.cs)|*.cs";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    txtFilePath.Text = sfd.FileName;
                }
            }
        }

        private void btnReplace_Click(object sender, EventArgs e)
        {
            try
            {
                foreach(var funcItem in funcDicList)
                {
                    if (File.Exists(txtFilePath.Text))
                    {
                        string[] lines = System.IO.File.ReadAllLines(txtFilePath.Text);
                        for (int i = 0; i < lines.Length; i++)
                        {
                            if (lines[i].Contains(funcItem.Key))
                            {
                                lines[i] = funcItem.Value(lines[i]);
                            }
                        }
                        File.WriteAllLines(txtFilePath.Text, lines);
                    }
                }
                MessageBox.Show("更新成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"更新失败！错误;{ex.Message}");
            }




        }

        /// <summary>
        /// 替换值
        /// </summary>
        /// <param name="strFilePath">txt等文件的路径</param>
        /// <param name="strIndex">索引的字符串，定位到某一行</param>
        /// <param name="newValue">替换新值</param>
        private static void ReplaceValue(string strFilePath, string findValue, string newValue)
        {
            if (File.Exists(strFilePath))
            {
                string[] lines = System.IO.File.ReadAllLines(strFilePath);
                for (int i = 0; i < lines.Length; i++)
                {
                    if (lines[i].Contains(findValue))
                    {
                        lines[i] = newValue;
                    }
                }
                File.WriteAllLines(strFilePath, lines);
            }
        }


        static string Replaceget__Default(string oldStr)
        {
            string keyStr = ".get__Default";
            if (!oldStr.Contains(keyStr)) return oldStr;
            StringBuilder newStr = new StringBuilder();
            int oldStrIndex = 0;
            bool isFindkey = false;
            while (oldStrIndex < oldStr.Length)
            {
                int keyStrIndex = 0;
                int tempoldStrIndex = oldStrIndex;
                while (keyStrIndex < keyStr.Length)
                {
                    if (oldStr[tempoldStrIndex] != keyStr[keyStrIndex]) break;
                    tempoldStrIndex++;
                    keyStrIndex++;
                }
                if (tempoldStrIndex == oldStrIndex
                    || keyStrIndex < keyStr.Length)
                {
                    //没匹配成功的情况
                    if (isFindkey)
                    {
                        if (oldStr[oldStrIndex] == ')')
                        {
                            newStr.Append("]");
                            isFindkey = false;
                        }
                        else
                        {
                            newStr.Append(oldStr[oldStrIndex]);
                        }
                    }
                    else
                    {
                        newStr.Append(oldStr[oldStrIndex]);
                    }
                }
                else
                {

                    newStr.Append("[");
                    isFindkey = true;
                    oldStrIndex = tempoldStrIndex;
                }
                oldStrIndex++;
            }


            return newStr.ToString();
        }

        static string Replaceset__Default(string oldStr)
        {
            string keyStr = ".set__Default";
            if (!oldStr.Contains(keyStr)) return oldStr;
            StringBuilder newStr = new StringBuilder();
            int oldStrIndex = 0;
            bool isFindkey = false;
            int commaChIndex = 0;
            int commaChlastIndex = oldStr.LastIndexOf(')');
            while (oldStrIndex < oldStr.Length)
            {
                int keyStrIndex = 0;
                int tempoldStrIndex = oldStrIndex;
                while (keyStrIndex < keyStr.Length)
                {
                    if (oldStr[tempoldStrIndex] != keyStr[keyStrIndex]) break;
                    tempoldStrIndex++;
                    keyStrIndex++;
                }
                if (tempoldStrIndex == oldStrIndex
                    || keyStrIndex < keyStr.Length)
                {
                    //没匹配成功的情况
                    if (isFindkey)
                    {
                        if (oldStr[oldStrIndex] == ',')
                        {
                            commaChIndex++;
                            if (commaChIndex == 1)
                            {
                                newStr.Append(oldStr[oldStrIndex]);
                            }
                            else
                            {
                                newStr.Append("]=");
                            }
                        }
                        else if (oldStrIndex == commaChlastIndex)
                        {
                            isFindkey = false;
                        }
                        else
                        {
                            newStr.Append(oldStr[oldStrIndex]);
                        }
                    }
                    else
                    {
                        newStr.Append(oldStr[oldStrIndex]);
                    }
                }
                else
                {

                    newStr.Append("[");
                    isFindkey = true;
                    oldStrIndex = tempoldStrIndex;
                }
                oldStrIndex++;
            }


            return newStr.ToString();
        }

        static string Replaceset__Value2(string oldStr)
        {
            string keyStr = ".set_Value2";
            if (!oldStr.Contains(keyStr)) return oldStr;
            StringBuilder newStr = new StringBuilder();
            int oldStrIndex = 0;
            bool isFindkey = false;
            int commaChFristIndex = oldStr.IndexOf('(');
            int commaChlastIndex = oldStr.LastIndexOf(')');
            while (oldStrIndex < oldStr.Length)
            {
                int keyStrIndex = 0;
                int tempoldStrIndex = oldStrIndex;
                while (keyStrIndex < keyStr.Length)
                {
                    if (oldStr[tempoldStrIndex] != keyStr[keyStrIndex]) break;
                    tempoldStrIndex++;
                    keyStrIndex++;
                }
                if (tempoldStrIndex == oldStrIndex
                    || keyStrIndex < keyStr.Length)
                {
                    //没匹配成功的情况
                    if (isFindkey)
                    {
                        if (oldStrIndex == commaChFristIndex)
                        {

                        }
                        else if (oldStrIndex == commaChlastIndex)
                        {
                            isFindkey = false;
                        }
                        else
                        {
                            newStr.Append(oldStr[oldStrIndex]);
                        }
                    }
                    else
                    {
                        newStr.Append(oldStr[oldStrIndex]);
                    }
                }
                else
                {

                    newStr.Append(".Value2=");
                    isFindkey = true;
                    oldStrIndex = tempoldStrIndex;
                }
                oldStrIndex++;
            }


            return newStr.ToString();
        }

        static string Replaceset_Name(string oldStr)
        {
            string keyStr = ".set_Name";
            if (!oldStr.Contains(keyStr)) return oldStr;
            StringBuilder newStr = new StringBuilder();
            int oldStrIndex = 0;
            bool isFindkey = false;
            int commaChFristIndex = oldStr.IndexOf('(');
            int commaChlastIndex = oldStr.LastIndexOf(')');
            while (oldStrIndex < oldStr.Length)
            {
                int keyStrIndex = 0;
                int tempoldStrIndex = oldStrIndex;
                while (keyStrIndex < keyStr.Length)
                {
                    if (oldStr[tempoldStrIndex] != keyStr[keyStrIndex]) break;
                    tempoldStrIndex++;
                    keyStrIndex++;
                }
                if (tempoldStrIndex == oldStrIndex
                    || keyStrIndex < keyStr.Length)
                {
                    //没匹配成功的情况
                    if (isFindkey)
                    {
                        if (oldStrIndex == commaChFristIndex)
                        {

                        }
                        else if (oldStrIndex == commaChlastIndex)
                        {
                            isFindkey = false;
                        }
                        else
                        {
                            newStr.Append(oldStr[oldStrIndex]);
                        }
                    }
                    else
                    {
                        newStr.Append(oldStr[oldStrIndex]);
                    }
                }
                else
                {

                    newStr.Append(".Name=");
                    isFindkey = true;
                    oldStrIndex = tempoldStrIndex;
                }
                oldStrIndex++;
            }


            return newStr.ToString();
        }
    }
}
